package com.ruoyi.project.asset.house.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.StartProcessInfo;
import com.bstek.uflo.service.TaskService;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.asset.assets.domain.Assets;
import com.ruoyi.project.asset.assets.model.constant.AssetsConstant;
import com.ruoyi.project.asset.assets.service.IAssetsService;
import com.ruoyi.project.asset.house.domain.House;
import com.ruoyi.project.asset.house.mapper.HouseMapper;
import com.ruoyi.project.asset.house.model.dto.HouseDTO;
import com.ruoyi.project.asset.house.model.vo.HouseVO;
import com.ruoyi.project.asset.house.service.IHouseService;
import com.ruoyi.project.system.uflo.model.constant.UfloConstant;
import com.ruoyi.project.system.user.domain.User;

/**
 * 房屋Service业务层处理
 * 
 * @author xianzw
 * @date 2020-08-24
 */
@Service
public class HouseServiceImpl  extends ServiceImpl<HouseMapper, House>  implements IHouseService 
{
    @Autowired
    private HouseMapper houseMapper;
    
    @Autowired
    private IAssetsService assetsService;
    
    @Autowired
    ProcessService processService;
    
    @Autowired
    TaskService taskService;

    /**
     * 查询房屋
     * 
     * @param id 房屋ID
     * @return 房屋
     */
    @Override
    public House selectHouseById(Long id)
    {
        return houseMapper.selectHouseById(id);
    }

    /**
     * 查询房屋列表
     * 
     * @param house 房屋
     * @return 房屋
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<HouseDTO> selectHouseList(HouseDTO houseDTO)
    {
        return houseMapper.selectHouseList(houseDTO);
    }

    /**
     * 新增房屋
     * 
     * @param house 房屋
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class,value = "mybatisTransactionManager")
    public Boolean insertHouse(HouseVO houseVO,User user)
    {
    	Assets assets = houseVO.houseVoToAssetsPo();
    	assets.setAssetsType(AssetsConstant.ASSETS_TYPE_HOUSE);
    	assets.setFlowState(UfloConstant.FLOW_STATE_0);
    	BeanUtils.setBean(assets, user);
    	boolean save = assetsService.save(assets);
    	
    	House house = houseVO.houseVoToHousePo();
    	house.setAssetsId(assets.getId());
    	BeanUtils.setBean(house, user);
    	this.save(house);
    	
        try {
        	StartProcessInfo info=new StartProcessInfo(user.getUserName());
            info.setBusinessId(assets.getId());
            info.setSubject("新增房屋--"+assets.getAssetsNo());
            info.setPromoter(user.getLoginName());
        	ProcessInstance processInstance = processService.startProcessByKey("house", info);
		} catch (Exception e) {
			throw new BusinessException("流程启动失败");
		}
        return true;
    }

    /**
     * 修改房屋
     * 
     * @param house 房屋
     * @return 结果
     */
    @Override
    public int updateHouse(House house)
    {
        return houseMapper.updateHouse(house);
    }

    /**
     * 删除房屋对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,value = "mybatisTransactionManager")
    public boolean deleteHouseByIds(String ids)
    {
    	String[] idArray = Convert.toStrArray(ids);
    	for (String id : idArray) {
    		this.deleteHouseById(id);
		}
    	return true;
    }

    /**
     * 删除房屋信息
     * 
     * @param id 房屋ID
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,value = "mybatisTransactionManager")
    public boolean deleteHouseById(String id)
    {
    	
    	Assets assets = assetsService.getById(id);
    	House house = this.getByAssetsId(id);
    	//删除流程实例
    	Task task = taskService.getTask(assets.getTaskId());
    	processService.deleteProcessInstanceById(task.getProcessInstanceId());
    	
    	//删除资产
    	assetsService.removeById(id);
    	//删除房屋
    	this.removeById(house.getId());
        return true;
    }

    /**
     * 根据资产id获取房屋
     */
	@Override
	public House getByAssetsId(String assetsId) {
		QueryWrapper<House> queryWrapper = new QueryWrapper<House>();
		queryWrapper.eq("assets_id", assetsId);
		return getOne(queryWrapper);
	}

	/**
	 * 根据资产id获取反房屋信息
	 */
	@Override
	public HouseDTO getHouseById(String assetsId) {
		Assets assets = assetsService.getById(assetsId);
		House house = this.getByAssetsId(assetsId);
		HouseDTO houseDTO = HouseDTO.assetsToHouseDTO(assets, house);
		return houseDTO;
	}
}
